package com.sqly.doc.menu.qianyue;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.ServiceRecord;
import com.sqly.doc.pojo.Team;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.util.DateConvert;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.XListView;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 选择用户界面
 * Created by Administrator on 2017-11-14.
 */

public class choose_team extends MyBaseActivity {


    List<Team> list;//信息列表
    XListView listView;
    ZLoadingDialog dialog;//加载动画
    Context con;
    private int pageNum1 = 1;// 无关键字页数
    EditText key;
    TextView title, uname;
    private ImageView cx;

    @Override
    protected void initUI() {
        setContentView(R.layout.team_choose);
        cx = (ImageView) findViewById(R.id.cx);
        cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum1 = 1;
                getTeam();
            }
        });
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("选择签约团队");
        uname.setVisibility(View.INVISIBLE);
        con = choose_team.this;
        key = (EditText) findViewById(R.id.key);
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(true);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色

    }

    @Override
    protected void initData() {
        if (NetWorkUtils.isNetworkConnected(con)) {//网络没有连接
            getTeam();
        }else{
            MyToastUtil.ShowToast(con, "获取错误");
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(con, getIntent().getStringExtra("cname").getClass());
                Bundle b = new Bundle();
                b.putSerializable("teamObj", list.get(i - 1));
                intent.putExtras(b);
                setResult(0, intent);
                finish();
            }
        });
    }

    @Override
    protected void initListener() {

    }

    //更新居民信息
    //网络请求居民信息
    public void getTeam() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("page", pageNum1 + "");
        ps.add("flag", "0");
        if (key.getText().toString().length() != 0)
            ps.add("XINGMING", key.getText().toString());

        UserClient.get(HttpUrl.teamByYsOrJmpagelst, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                MyToastUtil.ShowToast(con, "请求失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    if (pageNum1 == 1)
                        list = new ArrayList<Team>();
                    List<Team> ll = JSON.parseArray(ret.getList(), Team.class);
                    JSONArray js = JSON.parseArray(ret.getList());
                    for (int i = 0; i < js.size(); i++) {
                        JSONObject j = js.getJSONObject(i);
                        ll.get(i).setTid(j.getString("id"));
                    }
                    for (int i = 0; i < ll.size(); i++) {
                        list.add(ll.get(i));
                    }
                    listView.setSelection(list.size() - ll.size());
                    listView.stopRefresh();
                    listView.stopLoadMore();
                    listView.setRefreshTime("刚刚");
                    listView.setXListViewListener(new XListView.IXListViewListener() {
                        @Override
                        public void onRefresh() {
                            pageNum1 = 1;
                            getTeam();
                        }

                        @Override
                        public void onLoadMore() {
                            pageNum1 = pageNum1 + 1;
                            getTeam();
                        }
                    });
                    listView.setAdapter(new CommonAdapter<Team>(con, list, R.layout.team_choose_item) {
                        @Override
                        public void convert(ViewHolder helper, Team item) {
                            helper.setText(R.id.name, item.getName());
                            helper.setText(R.id.dwname, item.getNameD());
                        }
                    });
                } else {
                    MyToastUtil.ShowToast(con, "获取错误");
                }

            }
        });


    }
}
